package lamda;

import org.junit.Test;

import java.util.*;

public class Reduce {
    public static void main(String[] args) {
        int[] a={2,3,9,1};
        OptionalInt s = Arrays.stream(a).reduce((x, y) -> (x + y));

        int ff = Arrays.stream(a).reduce(3, (x, y) -> (x + y));
        System.out.println(s.getAsInt());
        System.out.println(ff);

    }
    @Test
    public void addList(){
        List<Integer> ss=new ArrayList<>();
        ss.add(3);ss.add(4);ss.add(6);
        Integer s1 = ss.stream().reduce(6, (x, y) -> (x * y));
        Optional<Integer> s2 = ss.stream().reduce((x, y) -> (x * y));
        Integer s22 = s2.get();
        System.out.println(s1);
        System.out.println(s22);
    }
    @Test
    public void addString(){
        List<String> ss=new ArrayList<>();
        ss.add("hah");ss.add("cdd");ss.add("cdd");

        String s1 = ss.stream().reduce("===", (x, y) -> (x + "***" + y));
        Optional<String> s2 = ss.stream().reduce((x, y) -> (x + "***" + y));
        String s22 = s2.get();
        System.out.println(s1);
        System.out.println(s22);
    }
    @Test
    public void addObjects(){
        List<BigClass> bs=new ArrayList<>();
        bs.add(new BigClass(1,1));
        bs.add(new BigClass(2,2));
        bs.add(new BigClass(3,3));
        BigClass reduce = bs.stream().reduce(new BigClass(-1, -1), (x, y) -> (new BigClass(x.n1 + y.n1, x.n2 + y.n2)));
        System.out.println(reduce);
    }
}
